import Util from "../../utils/util"
import api from '../../utils/api'
import moment from '../../utils/moment'
var app = getApp();
var token = wx.getStorageSync('token');
var theRankingType = 0;
var winHeight;
Page({
  data: {
    selected: true,
    selected1: false,
    worldList: [],
    friendsList: [],
    users: [],
    randNum: "",
    wordDataPage: 1,
    friendDataPage: 1,
    getAmount:0,
    shareUserId:0,
    share_amount:0
  },
  selected: function (e) {
    var _this = this;
    theRankingType = 0;
    this.setData({
      selected1: false,
      selected: true
    })
    _this.initFriend();
  },
  selected1: function (e) {
    var _this = this;
    theRankingType = 1;
    this.setData({
      selected: false,
      selected1: true
    });
    _this.initFriend();
  },
  initFriend: function () {
    var _this = this;
    api.post('index.php?g=User&m=Consumer&a=getUserRanking', {
      type: app.setConfig.type,
      token: token,
      rank_type: theRankingType,
      page: 1
    }, (data) => {
      if (theRankingType == 0) {
        _this.setData({
          friendsList: data.data.list,
          friendDataPage: 2,
          shareUserId: data.data.friend_id,
          share_amount: data.data.share_amount
        });

      } else {
        var randNum ="";
        var rankNum = parseInt(data.data.rankNum);
        if(rankNum == 0){
          randNum=1;
        } else if (rankNum > 0){
          randNum = rankNum+1;
        }else{
          randNum="未上榜";
        }
        console.log(data)
        _this.setData({
          worldList: data.data.list,
          randNum: randNum,
          users: data.data.users,
          wordDataPage: 2,
          shareUserId:data.data.friend_id,
          share_amount: data.data.share_amount
        });
      }

    }, false);
  },
  onLoad: function (options) {
    var _this = this;
    _this.initFriend();
    _this.share();
    wx.getSystemInfo({
      success: function (res) {
       var theWinHeight = res.windowHeight;
           _this.setData({
      winHeight: theWinHeight
    });

      }
    })
  },
    share:function(){
    //要求小程序返回分享目标信息
    wx.showShareMenu({
      withShareTicket: true
    });
  },
  onShareAppMessage: function (res) {
    var _this = this;
    if (res.from === 'button') {

      console.log(res.target)
    }
    return {
      title: '猜歌排行榜，看看你第几',
      path: '/pages/home/home?friendid=' + _this.data.shareUserId,
      success: function (res) {
        //onsole.log(res.shareTickets);
        if (res.shareTickets) {
          wx.getShareInfo({
            shareTicket: res.shareTickets[0],
            success(res) {
              var iv = res.iv;
              var encryptedData = res.encryptedData;
              wx.login({
                success: function (res) {
                  var code = res.code;
                  if (code) {
                    var shareParams = {
                      game_type: app.setConfig.type,
                      code: code,
                      iv: iv,
                      encryptedData: encryptedData,
                      token: token,
                      share_type: 1
                    }
                    api.post('index.php?g=Api&m=Share&a=groupShare', shareParams, function (res) {
                      if (res.code == 2000) {
                        _this.setData({
                          getAmount: res.amount
                        });
                        _this.dialogShare.showDialog();
                        console.log("分享返回")
                        console.log(res);
                      } else {
                        /*console.log("分享返回")
                        console.log(res);
                        wx.showLoading({
                          title: '分享成功',
                        })*/
                      }
                    },false)
                  }
                }
              });
            }
          });
        }else{
          //普通好友分享
          var shareParams = {
            game_type: app.setConfig.type,
            token: token,
            share_type: 0
          }
          api.post('index.php?g=Api&m=Share&a=groupShare', shareParams, function (res) {
           console.log(res);
            if (res.code == 2000) {        
              
              _this.setData({
                getAmount: res.amount,    
              });
              _this.dialogShare.showDialog();
              console.log(res);
            } else {
              console.log(res);
              wx.showLoading({
                title: '分享成功',
              })
            }
          }, false);
        }
      },
      fail: function (res) {
      }
    }
  },
  //加载更多朋友
  loadMoreFriendEvent: function () {
    var _this = this;
    var friendDataPage = _this.data.friendDataPage;
    var friendsList = _this.data.friendsList;
    wx.showLoading({
      title: '加载中',
    });
    api.post('index.php?g=User&m=Consumer&a=getUserRanking', {
      type: app.setConfig.type,
      token: token,
      rank_type: 0,
      page: friendDataPage
    }, (data) => {
      if(data.code==2000){
          for(var i=0;i<data.data.length;i++){
            friendsList.push(data.data.list[i]);
          }
          _this.setData({
            friendsList: friendsList,
            friendDataPage: friendDataPage,
            friendDataPage: parseInt(friendDataPage) + 1
          });
      }
      wx.hideLoading();
    }, false);
  },
  //加载更多世界
  loadMoreWordEvent: function () {
    
    var _this = this;
    var wordDataPage = _this.data.wordDataPage;
    var worldList = _this.data.worldList;
    api.post('index.php?g=User&m=Consumer&a=getUserRanking', {
      type: app.setConfig.type,
      token: token,
      rank_type: 1,
      page: wordDataPage
    }, (data) => {
      if(data.code==2000){
        for (var i = 0; i < data.data.list.length;i++){
          worldList.push(data.data.list[i]);
        }
        _this.setData({
          worldList: worldList,
          wordDataPage: wordDataPage,
          wordDataPage: parseInt(wordDataPage)+1
        });
        
      } 
     
    }, false);

  },
  onReady:function(e){
    this.dialogShare = this.selectComponent("#sharedialog");
  }
})